Getting started

Installation

You’ll need to install gganimate as follows.

Demo plots

Here are a couple plots that will serve as a basis for some examples, or can just give you a ready ggplot object to do your own exploration.

A starting point

We start with a simple example to get things going. Note that all animations will take at least several seconds to produce. To reduce this you can change the number of frames used, though this will typically make for a choppier plot. Note that output will be forced to the viewer instead of inline also.

To see what’s going on, let’s take it step by step. The transition_states function specifies how the plot information goes from one state to the next and how ‘states’ is defined. In this case, our states are represented by species. The transition_length is the ‘relative length of the transition’, but can be as many values as there are states. The state_length determines how long we pause (in terms of number of frames) at each state. We’ll talk about the others as we go along.

Typically you may want to assign the plot to an object so that you can use the animate function for more control.

Transitions

There are many transitions, though _states and _reveal are probably ones you’ll use a lot. The former works on some categorical variable as we saw before, while the latter works on an ordered (numeric) variable. A variant of it is transition_time, which can work with an actual date/time variable. In the following, we examine life expectancy over time with the gapminder data. We’ll see how to pause the visualization at the end for a specific number of frames.

We can use the glue package to insert text into the titles. Each type of transition you create comes with named objects we can use. With transition_time, the unique value is frame_time, but others are generically available. See the corresponding help files for what all is available.

Another that might come in handy is revealing the plot by the layers used to construct it. With the following we introduce some redundancy to take advantage of this.

The list of transitions includes:

Shadows

Shadows can add a nice effect to show paths over some index, especially time. The following recreates the famous TED talk visualization, which attempted to show how much better things were getting (if you ignored a lot of other factors).

The following shows the data for just a handful of countries. We’ll use both shadow_mark and shadow_trail. The former can shadow past vs. future data points, while the latter has additional options for leaving a trail of symbols behind.

Maybe things hadn’t changed that much after all for some, relatively speaking, or only improved in one domain.

Enter/Exit

We have many options for how the plots enter and leave the space. In the following, we use enter_drift to let the previous plot linger a bit.

The following might be fun as an exercise to demonstrate grow/shrink and, but please don’t do this to try and spruce up a boring bar graph. After running, set the size of enter_grow to 1 to see what it changes.

Here are the options we can play with. Each enter_* function has a corresponding exit_* function.

Easing

With easing we can define the velocity with which aesthetics change during an animation. This is done with ease_aes. By default the transition is ‘linear’, but let’s see some others.

Easing functions:

Modifiers

-in The easing function is applied as-is -out The easing function is applied in reverse -in-out The first half of the transition it is applied as-is, while in the last half it is reversed

Animate options

When using animate, you have some options you’ll commonly want to alter that will affect the animation appearance. Here are the ones you’ll probably want to fiddle with most.

Common issues

Simple breakage

Often you’ll just get an error. This means you’ll need to read the documentation closely, as you likely have not used the correct variable type, and in general have not provided the function what it expects.

As an example, let’s try to use transition_reveal for a categorical variable.

Error: along data must either be integer, numeric, POSIXct, Date, difftime, orhms

It states clearly what’s needed for this transition, and we didn’t provide it. We can change to transition_states for example.

But it worked before!

Sometimes what should work apparently does not. For example look at the following, which is our easing example from before. This time, no bounce!

The variables Why does this happen? Because behind the scenes some sort of grouping is being done on the data. By specifying the color argument, we only have the two groups, while we want each individual point to bounce. If we add a group = 1 aesthetic for geom_point as we did the first time, we can get what we want. As stated in the documentation, the group aesthetic defines how the data in a layer is matched across the animation.

R Markdown

You may find a number of issues arise when using the animations in R Markdown, especially with non-default device settings, and even if it runs fine with the chunk code. It may be easiest to save the plot and load the file instead.

Summary

Animation is a great way to enhance a plot and tell a data driven story. It can definitely be overkill for many situations though, and so should be used with caution. With the right approach though, you can take your visualizations further, and have some fun doing it too!